﻿using AuthenticationDemo.Common;
using AuthenticationDemo.Models.Base;
using AuthenticationDemo.Models.Enum;
using DockerProject.TokenValidation;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Text;

namespace AuthenticationDemo.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]

    //中间件则会启用双重认证，默认JWT
    //[Authorize]//如果开启 FallbackAuthenticationMiddleware 

    //使用特定身份认证
    //[Authorize(AuthenticationSchemes = CustomAuthenticationHandler.AuthenticationSchemeName)]

    //任一身份认证
    //[Authorize(AuthenticationSchemes = $"{CustomAuthenticationHandler.AuthenticationSchemeName},{JwtBearerDefaults.AuthenticationScheme}")]

    //使用特定策略身份认证    
    //[Authorize(policy:CustomPolicy.Policy_A)]
    [Authorize(policy: CustomPolicy.Policy_B)]
    public class DataProcessingController : ControllerBase
    {

        [HttpGet]
        public IEnumerable<ResponseBase<object>> GetUserData()
        {
            var fullPath = GetCompleteUrl(Request);

            return new[] {new ResponseBase<object>
            (
                code : ResponseCode.OK,
                message :"SUCCESS",
                success : true,
                data : new {
                        Id = Guid.NewGuid(),
                        Value = $"当前时间是：{DateTime.Now.DateTimeToString()} ",
                        RequestUrl = fullPath,
                        ServerId = App.Configuration["ServerId"],
                        Code = Guid.NewGuid().ToString("N")
                    }
            )};
        }

        private string GetCompleteUrl(HttpRequest source)
        {
            return new StringBuilder()
                   .Append(source.Scheme)
                   .Append("://")
                   .Append(source.Host)
                   .Append(source.PathBase)
                   .Append(source.Path)
                   .Append(source.QueryString)
                   .ToString();
        }
    }
}
